package com.dbq.fast.business.setting;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.SpannableStringBuilder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dbq.fast.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 */

public class UpdateDialog extends DialogFragment {

    public static final int UPDATE_VERSION = 1040; //Y page hint

    private int status;
    private String content;
    private String version;

    private Unbinder unbinder;
    private OnUpdateListener ylistener;
    @BindView(R.id.tv_title)
    TextView titleTv;
    @BindView(R.id.tv_version)
    TextView versionTv;

    @BindView(R.id.tv_content)
    TextView contentTv;

    @BindView(R.id.tv_left)
    TextView leftTv;

    @BindView(R.id.tv_right)
    TextView rightTv;

    @BindView(R.id.center_line)
    View centerLine;

    @BindView(R.id.ll_bottom)
    LinearLayout bottomLl;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0x00ffffff));
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_update, container, true);
        unbinder = ButterKnife.bind(this, view);
        setPayStatus(status, content);
        getDialog().setCanceledOnTouchOutside(false);
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        status = getArguments().getInt("status");
        content = getArguments().getString("content");
        version = getArguments().getString("version");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
//        countDownlistener = null;
//        btmListener = null;
    }


    private static UpdateDialog newInstance(int status, String content, String version) {
        UpdateDialog payAndLoginStatusDialog = new UpdateDialog();
        Bundle args = new Bundle();
        args.putInt("status", status);
        args.putString("content", content);
        args.putString("version", version);
        payAndLoginStatusDialog.setArguments(args);

        return payAndLoginStatusDialog;
    }


    public static UpdateDialog show(FragmentManager fragmentManager, int status) {
        return show(fragmentManager, status, "", "");
    }

    public static UpdateDialog show(FragmentManager fragmentManager, int status, String content, String version) {
        FragmentTransaction ft = fragmentManager.beginTransaction();
        UpdateDialog newFragment = UpdateDialog.newInstance(status, content, version);
        ft.add(newFragment, "pay_login_dialog");

        ft.commitAllowingStateLoss();
        // Create and show the dialog.
        isShowing = true;
        return newFragment;
    }

    static boolean isShowing = false;

    public static boolean isShowing() {
        return isShowing;
    }

    private void setPayStatus(int status, String content) {
        if (!isAdded()) return;
        switch (status) {
            case UPDATE_VERSION:
                setTitle(R.string.have_new_version);
                setContent(content);
                versionTv.setText(version);
                setLeftTv(R.string.update_cancel);
                setRightTv(R.string.update);
                setLeftVisible(View.VISIBLE);
                setLineVisible(View.VISIBLE);
                setRightVisible(View.VISIBLE);
                leftTv.setOnClickListener(v -> {
                    if (ylistener != null) {
                        ylistener.cancel();
                    }
                    isShowing = false;
                    this.dismiss();
                });
                rightTv.setOnClickListener(v -> {
                    if (ylistener != null) {
                        ylistener.nextPage();
                        isShowing = false;
                        this.dismiss();
                    }
                });
                break;
        }
    }

    private void setTitle(int strId) {
        if (titleTv != null) {
            titleTv.setText(strId);
        }
    }

    private void setContent(SpannableStringBuilder style) {
        if (contentTv != null) {
            contentTv.setText(style);
        }
    }

    private void setContent(String content) {
        if (contentTv != null) {
            contentTv.setText(content);
        }
    }

    private void setRightTv(int strId) {
        if (rightTv != null) {
            rightTv.setText(strId);
        }
    }

    private void setLeftTv(int strId) {
        if (leftTv != null) {
            leftTv.setText(strId);
        }
    }

    private void setLineVisible(int visible) {
        if (centerLine != null) {
            centerLine.setVisibility(visible);
        }
    }

    private void setRightVisible(int visible) {
        if (rightTv != null) {
            rightTv.setVisibility(visible);
        }
    }

    private void setLeftVisible(int visible) {
        if (leftTv != null) {
            leftTv.setVisibility(visible);
        }
    }

    private void setBottomVisible(int visible) {
        if (bottomLl != null) {
            bottomLl.setVisibility(visible);
        }
    }


    public void setUpdateListener(OnUpdateListener listener) {
        this.ylistener = listener;
    }

    public interface OnUpdateListener {
        void nextPage();

        void cancel();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        // super.onSaveInstanceState(outState); 不保存用户状态
    }

}
